package com.example.service.impl;

import com.example.mapper.ApartmentMapper;
import com.example.mapper.MaintainMapper;
import com.example.mapper.MaintenancestaffMapper;
import com.example.mapper.UserMapper;
import com.example.pojo.Apartment;
import com.example.pojo.Maintain;
import com.example.pojo.Maintenancestaff;
import com.example.pojo.User;
import com.example.service.MaintainService;
import com.example.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class MaintainServiceImpl implements MaintainService {
    @Autowired
    private MaintainMapper maintainMapper;

    @Autowired
    private ApartmentMapper apartmentMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MaintenancestaffMapper maintenancestaffMapper;

    @Override
    public void addServiceApartment(Maintain maintain, HttpServletRequest request) {
        User pim = userMapper.getPim(JwtUtils.parseJWT(request.getHeader("token")).get("id", Integer.class));
        Apartment apartmentId = apartmentMapper.getApartmentId(maintain.getIdA());
        User user = userMapper.getPim(apartmentId.getIdU());
        maintain.setTnumber(pim.getNumber());
        maintain.setTname(pim.getNickname());
        maintain.setTDate(LocalDateTime.now());
        maintain.setFdname(user.getNickname());
        maintain.setName(apartmentId.getName());
        maintain.setFyimage(apartmentId.getImage());
        maintain.setLocation(apartmentId.getLocation());
        maintain.setIdU(JwtUtils.parseJWT(request.getHeader("token")).get("id", Integer.class));
        maintain.setIdF(user.getId());
        maintainMapper.addServiceApartment(maintain);
    }

    @Override
    public void setServiceman(Maintain maintain, HttpServletRequest request) {
        maintain.setIdF(JwtUtils.parseJWT(request.getHeader("token")).get("id",Integer.class));
        Maintenancestaff maintenancestaff=maintenancestaffMapper.getM(maintain.getPdId());
        maintain.setPname(maintenancestaff.getStaffName());
        maintain.setPnumber(maintenancestaff.getStaffPhone());
        maintainMapper.setServiceman(maintain);
        maintenancestaffMapper.setType(maintain.getPdId());

    }

    @Override
    public List<Maintain> getServiceApartmentZK(HttpServletRequest request) {
        Integer idU = JwtUtils.parseJWT(request.getHeader("token")).get("id", Integer.class);
        List<Maintain> list=maintainMapper.getServiceApartmentZK(idU);
        return list;
    }

    @Override
    public List<Maintain> getServiceApartmentFD(String name,Integer sfpd, HttpServletRequest request) {
        Integer idF = JwtUtils.parseJWT(request.getHeader("token")).get("id", Integer.class);
        List<Maintain> list=maintainMapper.getServiceApartmentFD(name,sfpd,idF);
        return list;
    }

    @Override
    public void setRepair(Integer id, HttpServletRequest request) {
        Integer idF = JwtUtils.parseJWT(request.getHeader("token")).get("id", Integer.class);
        maintainMapper.setRepair(id,idF);
        Integer staffId=maintainMapper.getServiceApartment(id);
        maintenancestaffMapper.pdType(staffId);
    }


}
